/**
 * Created by Mark_ren on 2016/11/28.
 * 苟富贵,無相忘
 */
var moment = require("moment");
var co = require('co');
var EventProxy = require('eventproxy');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');


var pfLogController = {
    addMore: function (req, res) {
        co(function () {
            var params = req.query.params;
            var totalScore = req.query.totalScore;
            var hospitalName = req.query.hospitalName;
            var arrParams = JSON.parse(params);
            var nowTime = util.getNowTime();
            var paramT = {};
            var openId = "";
            var playerId = "";
            var pfLogSql = "insert into pf_log values ";
            arrParams.forEach(function (obj) {
                openId = obj.judgeOpenId;
                playerId = obj.playerId;
                pfLogSql += "('" + util.getUUID() + "','" + obj.judgeOpenId + "','" + obj.playerId + "','" + obj.meetNum + "','" + obj.questionId + "','" + obj.checkId + "','" + obj.score + "','" + nowTime + "'),";
            });
            paramT.id = util.getUUID();
            paramT.judgeOpenId = openId;
            paramT.playerId = playerId;
            paramT.totalScore = totalScore;
            paramT.createTime = util.getNowTime();
            pfLogSql = pfLogSql.substring(0, pfLogSql.lastIndexOf(","));
            seq.pfMemberModule.findOne({ where: { "openId": openId } }).then(function (data) {
                if (data != null && data != undefined) {
                    seq.pfLogModule.count({ where: { "judgeOpenId": openId, "playerId": playerId } }).then(function (count) {
                        if (count == 0) {
                            if (hospitalName == data.hospitalName) {
                                res.json({ 'success': false, 'msg': "您与选手所在的医院相同，不能进行评分！" });
                            } else {
                                seq.sequelize.query(pfLogSql).spread(function (result) {
                                    seq.pfTotalScoreModule.create(paramT).then(function (totalResult) {
                                        res.json({ 'success': true, 'msg': '添加成功' });
                                    })
                                })
                            }

                        } else {
                            res.json({ 'success': false, 'msg': '您已经对该选手进行评分！' });
                        }
                    })
                } else {
                    res.json({ 'success': false, 'msg': '您不是评委不能进行评分！' });
                }
            })
        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '添加失败' });
            console.log(e);
        })
    }
}
module.exports = pfLogController;